﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using V5_EaspLibs;
using System.Web.UI;
using System.IO;
using System.Web;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Xml;
using V5_EaspLibs.FrameWork.Model;

namespace V5_FrameWork.Data {
    /// <summary>
    /// 部件管理
    /// </summary>
    public class BaseWidgetHelper {
        /// <summary>
        /// 部件路径
        /// </summary>
        private string m_XmlWidgetPath = HttpContext.Current.Server.MapPath("~/") + "Config/Widget.config";
        /// <summary>
        /// 载入部件
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public string Include(string filePath) {
            return Easp.Html.Include(filePath);
        }
        /// <summary>
        /// 载入部件 并传参数
        /// </summary>
        /// <param name="filePath">ascx文件地址~/Widget/Article/1.ascx   文件路径为1.ascx</param>
        /// <param name="widgetDir">部件目录 做唯一用</param>
        /// <param name="_params">参数</param>
        /// <returns></returns>
        public string Include(string filePath, string widgetDir, string _params) {
            try {
                //Page page = (Page)HttpContext.Current.Handler;
                Page page = new Page();
                UserControl ctl = (UserControl)page.LoadControl("~/Widget/" + widgetDir + "/" + filePath);
                //UserControl ctl = (UserControl)page.LoadControl("~/" + filePath);
                Type ucType = ctl.GetType();
                #region 设置属性
                if (!string.IsNullOrEmpty(_params)) {
                    try {
                        PropertyInfo ucProperty = ucType.GetProperty("Params");
                        ucProperty.SetValue(ctl, _params, null);
                    }
                    catch { }
                }
                if (!string.IsNullOrEmpty(widgetDir)) {
                    try {
                        PropertyInfo ucProperty = ucType.GetProperty("WidgetDir");
                        ucProperty.SetValue(ctl, widgetDir, null);
                    }
                    catch { }
                }
                #endregion
                page.Controls.Add(ctl);
                StringWriter writer = new StringWriter();
                HttpContext.Current.Server.Execute(page, writer, true);
                return writer.ToString();
            }
            catch (Exception ex) { return widgetDir + filePath + "加载失败!" + ex.Message + ex.InnerException; }
        }

        /// <summary>
        /// 获取部件 格式化表达式
        /// </summary>
        /// <param name="htmlTemplate"></param>
        /// <returns></returns>
        public string PraseTagMode(string htmlTemplate) {
            string pattern = @"<V5:Widget>([\S\s]*?)</V5:Widget>";
            MatchCollection matchs = new Regex(pattern, RegexOptions.IgnoreCase).Matches(htmlTemplate);
            foreach (Match m in matchs) {
                string tagAll = m.Groups[0].Value;
                string tagParams = m.Groups[1].Value;
                string file = Easp.Html.TagVal(tagParams, "FilePath");
                string widgetdir = Easp.Html.TagVal(tagParams, "WidgetDir");
                string temp = Include(file, widgetdir, tagParams);
                htmlTemplate = Easp.Str.Replace(htmlTemplate, tagAll, temp);
            }
            return htmlTemplate;
        }

        /// <summary>
        /// 获取所有部件列表
        /// </summary>
        /// <returns></returns>
        public List<ModelWidget> GetAllWidgetItemHelperList() {
            List<ModelWidget> ListWidgetItemHelper = new List<ModelWidget>();
            XmlDocument xml = new XmlDocument();
            xml.Load(m_XmlWidgetPath);
            XmlNodeList NodesList = xml.SelectNodes("widget/widgetitem");
            if (NodesList != null) {
                ModelWidget model;
                foreach (XmlNode node in NodesList) {
                    model = new ModelWidget();
                    model.Name = node.Attributes["name"].Value;
                    model.Label = node.Attributes["label"].Value;
                    model.Description = node.Attributes["description"].Value;
                    model.Icon = node.Attributes["icon"].Value;
                    model.DefaultType = node.Attributes["defaulttype"].Value;
                    model.WidgetDir = node.Attributes["widgetdir"].Value;
                    #region 类型
                    XmlNodeList NodesListType = node.SelectNodes("widget/widgetitem/type");
                    if (NodesListType != null) {
                        ModelWidget.WidgetItemType m;
                        foreach (XmlNode nodeType in NodesListType) {
                            m = new ModelWidget.WidgetItemType();
                            m.Name = nodeType.Attributes["defaulttype"].Value;
                            m.Label = nodeType.Attributes["label"].Value;
                            m.File = nodeType.Attributes["file"].Value;
                            model.ListWidgetItemType.Add(m);
                        }
                    }
                    #endregion
                    ListWidgetItemHelper.Add(model);
                }
            }
            return ListWidgetItemHelper;
        }
    }
}
